var guanpai = require('guanpai');

cc.Class({
    extends: cc.Component,

    properties: {
        loginPanel: cc.Node,
        usernameEditBox: cc.EditBox,
        passwordEditBox: cc.EditBox,
        confirmPasswordEditBox: cc.EditBox,
    },

    // use this for initialization
    onLoad: function () {

    },

    switchToLoginPanel: function () {
        this.loginPanel.active = true;
        this.node.active = false;
    },

    onClickRegister: function () {
        var username = this.usernameEditBox.string;
        var password = this.passwordEditBox.string;
        var confirmPassword = this.confirmPasswordEditBox.string;
        if (username.length <= 0) {
            guanpai.ui.createScreenMsg("用户名不能为空");
            return;
        }
        else if (password.length <= 0) {
            guanpai.ui.createScreenMsg("密码不能为空");
            return;
        }
        else if (password != confirmPassword) {
            guanpai.ui.createScreenMsg("两次输入的密码不一致");
        }
        else {
            guanpai.netEngine.send("register", {username: username, password: password}, (reply)=>{
                cc.log("qiaqia: " + JSON.stringify(reply));
                if (!reply.errorCode) {
                    guanpai.ui.createScreenMsg("用户" + reply.username + "注册成功");
                }
                else {
                    switch (reply.errorCode) {
                        case 1:
                            guanpai.ui.createScreenMsg("用户名已存在");
                            break;
                        default:
                            guanpai.ui.createScreenMsg("未知错误");
                            break;
                    }
                }
            });
        }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
